package com.uwlaser.rest;

import com.uwlaser.annotation.Log;
import com.uwlaser.domain.CraftOperation;
import com.uwlaser.service.CraftOperationService;
import com.uwlaser.service.dto.CraftOperationDto;
import com.uwlaser.service.dto.CraftOperationQueryParam;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.util.Set;

/**
* @author zk-Qin
* @date 2022-05-11
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "工序管理管理")
@RequestMapping("/api/craftOperation")
public class CraftOperationController {

    private final CraftOperationService craftOperationService;

    @GetMapping
    @Log("查询工序管理")
    @ApiOperation("查询工序管理")
    @PreAuthorize("@el.check('craftOperation:list')")
    public ResponseEntity query(CraftOperationQueryParam query, Pageable pageable){
        return new ResponseEntity<>(craftOperationService.queryAll(query,pageable),HttpStatus.OK);
    }

    @GetMapping("/list")
    @Log("查询工序列表")
    @ApiOperation("查询工序列表")
    @PreAuthorize("@el.check('craftOperation:list')")
    public ResponseEntity list(CraftOperationQueryParam query){
        return new ResponseEntity<>(craftOperationService.queryAll(query),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增工序管理")
    @ApiOperation("新增工序管理")
    @PreAuthorize("@el.check('craftOperation:add')")
    public ResponseEntity create(@Validated @RequestBody CraftOperationDto resources){
        return new ResponseEntity<>(craftOperationService.insert(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改工序管理")
    @ApiOperation("修改工序管理")
    @PreAuthorize("@el.check('craftOperation:edit')")
    public ResponseEntity update(@Validated @RequestBody CraftOperationDto resources){
        craftOperationService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除工序管理")
    @ApiOperation("删除工序管理")
    @PreAuthorize("@el.check('craftOperation:del')")
    public ResponseEntity delete(@RequestBody Set<Integer> ids) {
        craftOperationService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /*
    @Log("导出工序管理")
    @ApiOperation("导出工序管理")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('craftOperation:list')")
    public void download(HttpServletResponse response, CraftOperationQueryParam query) throws IOException {
        craftOperationService.download(craftOperationService.queryAll(query), response);
    }*/

}
